package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    // 查询推荐视频
    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoVoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        PageBeanVo pageBeanVo = videoManager.findRecommendVideoVoByPage(userId, pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    // 发布视频
    @PostMapping("/smallVideos")
    public void publishVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.publishVideo(userId, videoThumbnail, videoFile);
    }

    // 关注用户
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void saveFocusUser(@PathVariable Long focusUserId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.saveFocusUser(userId, focusUserId);
    }

    // 取消关注
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void removeFocusUser(@PathVariable Long focusUserId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.removeFocusUser(userId, focusUserId);
    }


    //视频点赞
    @PostMapping(value = "/smallVideos/{id}/dislike")
    public ResponseEntity videoIdLike(@PathVariable("id") String videoId) {
        return videoManager.videoLike(videoId);
    }
    //视频取消点赞
    @PostMapping(value = "/smallVideos/{id}/like")
    public ResponseEntity dislike(@PathVariable("id") String publishId){
        return videoManager.dislike(publishId);
    }
    //查看评论列表
    @GetMapping(value = "/smallVideos/{id}/comments")
    public ResponseEntity selectComments(@PathVariable("id")String videoId,
                                         @RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) throws Exception {

        return videoManager.selectVideoComments(videoId,page,pagesize);
    }
    //POST/smallVideos/:id/comments
    //评论发布
    @PostMapping(value ="/smallVideos/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id")String videoId,@RequestBody Map map){
        String comment = (String) map.get("comment");
        return videoManager.saveComment(videoId,comment);

    }
    //视频评论点赞
    @PostMapping(value ="/smallVideos/comments/{id}/like")
    public ResponseEntity commentLike(@PathVariable("id")String commentId){
        return videoManager.commentLike(commentId);

    }
    //视频评论取消点赞
    @PostMapping(value = "/smallVideos/comments/{id}/dislike")
    public ResponseEntity commentDislike( @PathVariable("id") String publishId) throws Exception{//评论id
        return videoManager.commentDislike(publishId);
    }
}
